package com.itheima.controller;

import com.itheima.anno.Mylog;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Vip;
import com.itheima.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/vips")
public class VipController {

    @Autowired
    private VipService vipService;

    // 添加会员
    @PostMapping
    public Result save(@RequestBody Vip vip) {
        vipService.save(vip);
        return Result.success();
    }

    // 查询会员
    @GetMapping
    public Result page(String name,
                       @RequestParam int page, @RequestParam int rows) {
        PageBean pageBean = vipService.page(name,page,rows);
        return Result.success(pageBean);
    }

    // 根据ID查找
    @GetMapping("/{id}")
    public Result getVipById(@PathVariable Integer id) {
      Vip vip = vipService.getVipById(id);
      return Result.success(vip);
    }

    // 修改会员
    @Mylog
    @PutMapping
    public Result updateVip(@RequestBody Vip vip) {
       vipService.update(vip);
       return Result.success();
    }

    // 删除会员
    @Mylog
    @DeleteMapping
    public Result deleteUser(@RequestParam List<Integer> ids) {
        vipService.delete(ids);
        return Result.success();
    }
}
